﻿@using Signum.Entities.DynamicQuery
@using Signum.Entities.Reflection
@using Signum.Web.Properties
@using Signum.Engine
@using Signum.Utilities.DataStructures
@model Context

@{ 
   int elems = (int)ViewData[ViewDataKeys.ElementsPerPage];
   FilterMode filterMode = (FilterMode)ViewData[ViewDataKeys.FilterMode];
   int columnsCount = (int)ViewData[ViewDataKeys.SearchControlColumnsCount];

   ResultTable resultTable = (ResultTable)ViewData[ViewDataKeys.Results];
}

<tr class="ui-widget-header ui-corner-bottom sf-search-footer" style="@((filterMode == FilterMode.OnlyResults) ?  "display:none" : "")">
    <td colspan="@columnsCount" class="sf-search-footer-pagination">
        <div class="sf-pagination-left">
            @if (resultTable != null)
            {
                if (resultTable.TotalPages > 1)
                {
                    <span class="sf-pagination-strong">@resultTable.StartElementIndex.ToString()</span>
                    <span> - </span>
                    <span class="sf-pagination-strong">@resultTable.EndElementIndex.ToString()</span>
                    <span>@(" {0} ".Formato(Signum.Web.Properties.Resources.SearchControl_Pagination_Of))</span>
                }
                <span class="sf-pagination-strong">@resultTable.TotalElements.ToString()</span>
                <span>@(" {0}.".Formato(Resources.SearchControl_Pagination_Results))</span>
            }
        </div>
        <div class="sf-pagination-center">
            <span>@Resources.SearchControl_Pagination_Rows</span>
            @{ 
                var topItems = new List<int> { 5, 10, 20, 50, 100, 200 }.Select(i => new SelectListItem { Text = i.ToString(), Value = i.ToString(), Selected = i.ToString() == elems.TryToString() }).ToList();
                topItems.Add(new SelectListItem { Text = Resources.SearchControl_Pagination_All, Value = "-1", Selected = elems == -1 });
            }
            @Html.DropDownList(Model.Compose("sfElems"), topItems, new { @class = "sf-pagination-size" })
        </div>
        <div class="sf-pagination-right">
            @if (resultTable != null)
            {
                <input type="hidden" id="@(Model.Compose("sfPage"))" value="@resultTable.CurrentPage" />
                <input type="button" class="sf-pagination-button" data-page="@(resultTable.CurrentPage - 1)" @((resultTable.CurrentPage <= 1) ? "disabled=\"disabled\"" : "") value="&lt;" />
            
                MinMax<int> interval = new MinMax<int>(
                    Math.Max(1, resultTable.CurrentPage - 2),
                    Math.Min(resultTable.CurrentPage + 2, resultTable.TotalPages));

                if (interval.Min != 1)
                {
                    <input type="button" class="sf-pagination-button" data-page="1" value="1" />
                    if (interval.Min - 1 != 1)
                    {
                        <span>...</span>
                    }
                }

                for (int i = interval.Min; i < resultTable.CurrentPage; i++)
                {
                    <input type="button" class="sf-pagination-button" data-page="@i" value="@i" />
                }

                <span class="sf-pagination-strong">@resultTable.CurrentPage.ToString()</span>

                for (int i = resultTable.CurrentPage + 1; i <= interval.Max; i++)
                {
                    <input type="button" class="sf-pagination-button" data-page="@i" value="@i" />
                }

                if (interval.Max != resultTable.TotalPages)
                {
                    if (interval.Max + 1 != resultTable.TotalPages)
                    {
                        <span>...</span>
                    }
                    <input type="button" class="sf-pagination-button" data-page="@resultTable.TotalPages" value="@resultTable.TotalPages" />
                }
            
                <input type="button" class="sf-pagination-button" data-page="@(resultTable.CurrentPage + 1)" @((resultTable.TotalPages <= resultTable.CurrentPage) ? "disabled=\"disabled\"" : "") value="&gt;" />
            }
        </div>
    </td>
</tr>